package com.example.spring_xx_u_market;

import com.example.bean.ErrorCodeV;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.UidCtrl;

import javax.servlet.http.HttpServletRequest;

import static com.example.spring_xx_u_market.SpringXxUMarketApplication.jdbcTemplate;

@Slf4j
@RestController
@RequestMapping("api")
public class SettingCtrl {
    @PostMapping("setting")
    public Object getSetting(HttpServletRequest request) {
        String uid = request.getParameter("uid");
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String rec_address = request.getParameter("Rec_address");

        if (uid == null||name==null||phone==null||email==null||rec_address==null) {
            return new ErrorCodeV("005");
        } else {
            switch (UidCtrl.checkUid(uid)) {
                case "-2":
                    return new ErrorCodeV("003");
                case "-1":
                    return new ErrorCodeV("002");
                case "0":
                    return new ErrorCodeV("004");
                default: {
                    int update = jdbcTemplate.update("update second_hand.user_info set nickname =?,phone=?,email=?,rec_address=? where account= (select uid.account from second_hand.uid where uid=?)", name, phone, email, rec_address, uid);
                    if (update==0){
                        return new ErrorCodeV("007");
                    }
                    return new ErrorCodeV("0");
                }
            }
        }
    }
}
